//
//  XHomeDropCell.m
//  Bees
//
//  Created by Xiaohan on 2018/12/18.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "XHomeDropCell.h"
#import "FoundModel.h"
@interface XHomeDropCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView*collectionView;
@property (nonatomic,strong) UICollectionViewFlowLayout*flowLayout;
@end

@implementation XHomeDropCell
- (void)setDatas:(NSArray<FoundRecommendModel *> *)datas{
    _datas = datas;
    [self.collectionView reloadData];
}
- (void)setupUI{
    self.contentView.backgroundColor = UIColor.clearColor;
    self.backgroundColor = UIColor.clearColor;
    [self.contentView addSubview:self.collectionView];
}

- (void)layoutSubviews{
    self.collectionView.frame = self.bounds;
}

#pragma mark - delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.datas.count>8?8:self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XHomeDropClvCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XHomeDropClvCell class]) forIndexPath:indexPath];
    FoundRecommendModel*m = [self.datas objectOrNilAtIndex:indexPath.item];;
    [cell.iconV sd_setImageWithURL:String_To_URL(m.logo) placeholderImage:nil];
    cell.titleLab.text = m.name;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    !self.clickItemBlock?:self.clickItemBlock([self.datas objectOrNilAtIndex:indexPath.item]);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark - getter
- (UICollectionView*)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[XHomeDropClvCell class] forCellWithReuseIdentifier:NSStringFromClass([XHomeDropClvCell class])];
        _collectionView.pagingEnabled = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.contentInset = UIEdgeInsetsZero;
    }
    return _collectionView;
}
- (UICollectionViewFlowLayout*)flowLayout{
    if (!_flowLayout) {
        _flowLayout = [UICollectionViewFlowLayout new];
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _flowLayout.itemSize = CGSizeMake(kScreenWidth/4.0f, kXHomeDropClvCellHeight);
        _flowLayout.minimumLineSpacing=0.0f;
        _flowLayout.minimumInteritemSpacing= 0.0f;
    }
    return _flowLayout;
}
@end


#pragma mark - 自定义视图
@interface XHomeDropClvCell()

@end

@implementation XHomeDropClvCell
- (void)setupUI{
    self.contentView.backgroundColor = UIColor.clearColor;
    self.backgroundColor = UIColor.clearColor;
    [self.contentView addSubview:self.iconV];
    [self.iconV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(50.0f), AdaptedWidth(50.0f)));
        make.centerX.mas_equalTo(0);
    }];
    [self.contentView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(3);
        make.right.mas_equalTo(-3);
    }];
}
- (UIImageView*)iconV{
    if (!_iconV) {
        _iconV = [UIImageView new];
        ViewBorderRadius(_iconV, AdaptedWidth(13.0f), 1, kLineColor);
    }
    return _iconV;
}

- (UILabel*)titleLab{
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.textColor = UIColor.whiteColor;
        _titleLab.font = kDefaultFont(12);
        _titleLab.textAlignment=NSTextAlignmentCenter;
    }
    return _titleLab;
}
@end
